package sing.myapp.router

import android.app.Activity
import android.net.Uri
import android.os.Bundle
import androidx.fragment.app.Fragment
import com.alibaba.android.arouter.launcher.ARouter
import sing.core.Global

object JumpCenter {

    private fun toJump(activity: Activity? = null, path: String, requestCode: Int = -1, bundle: Bundle = Bundle()): Any? {
        if (activity == null){
            return ARouter.getInstance().build(path).with(bundle).navigation()
        }else{
            return ARouter.getInstance().build(path).with(bundle).navigation(activity, requestCode, null)
        }
    }

    // 登录
    fun toLogin(): Any? {
        return toJump(path = RouterURL.PAGE_LOGIN)
    }

    fun getH5Fragment(url: String?, title: String = "", showStatus: Boolean = false, showBack: Boolean = false, showTitle: Boolean = false,isBlackBack :Boolean = true): Fragment {
        val realTitle = if (title == "") {
            "标题"
        } else {
            title
        }

        val bundle = Bundle()
        bundle.putString("url", url)
        bundle.putString("title", realTitle)
        bundle.putBoolean("showTitle", showTitle)
        bundle.putBoolean("showStatus", showStatus)
        bundle.putBoolean("showBack", showBack)
        bundle.putBoolean("isBlackBack", isBlackBack)
        return ARouter.getInstance().build(RouterURL.H5_FRAGMENT_WEB_VIEW).with(bundle).navigation() as Fragment
    }

    // 去网页
    fun toWebView(url: String?, title: String = "", showStatus: Boolean = true, showBack: Boolean = true,showTitle: Boolean = false, isTxt: Boolean = false,isBlackBack :Boolean = true): Any? {
        val realTitle = if (title == "") {
            "标题"
        } else {
            title
        }

        var u = url
        if (!isTxt) { // 二维码扫描出的是纯文本，直接展示，其余的加在http头
            if (!"$url".startsWith("http") && !"$url".startsWith("file")) {
                u = "${Global.H5_URL}/$url"
            }
        }

        val bundle = Bundle()
        bundle.putString("url", Uri.encode(u))
        bundle.putString("title", realTitle)
        bundle.putBoolean("showTitle", showTitle)
        bundle.putBoolean("showStatus", showStatus)
        bundle.putBoolean("showBack", showBack)
        bundle.putBoolean("isBlackBack", isBlackBack)
        return toJump(path = RouterURL.H5_ACTIVITY_WEB_VIEW, bundle = bundle)
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    fun toCalc():Any? {
        return toJump(path = RouterURL.PAGE_CALC)
    }

    fun toWebView(url: String, title: String):Any? {
        return toWebView(url,title, showTitle = true)
    }
}